package com.example.mapreduce.combiner;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * ClassName: WordCountCombiner
 * Package: com.example.mapreduce.combiner
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-16
 * Time: 19:50
 */

/**
 * 自定义Combiner 也是继承Reduce 并且重写reduce方法
 * Combiner是在Map阶段 输入的kv就是map输出的kv
 * 写完之后在驱动类里面关联起来
 */
public class WordCountCombiner extends Reducer<Text, IntWritable,Text,IntWritable> {

    private IntWritable outV = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

        //也是对单词进行统计 在正式的reduce之前 Combiner统计
        int sum = 0;

        for (IntWritable word : values){

            sum += word.get();

        }

        outV.set(sum);

        context.write(key,outV);

    }
}
